# Python 2.6
from Tkinter   import *
from tkFileDialog import askopenfilename
from tkFileDialog import askdirectory
import os

# 'Database' class, a database with certain input functions and storage methods / variables.
# The parameter 'master' is a reference to the master Tk() instance (the window). It can be forced to close
# and things with for example: self.master.destroy()
class db:
    # Path being worked on, will be either a file or a directory
    currentpaths = []
    
    def __init__(self, master):
        # store the master in local db variable so we can access it later on
        self.master = master

    def pickfile(self):
        # Call "Choose file..." dialog
        path = askopenfilename(filetypes=[('All supported', '.jpg .jpeg .png .gif .tif'), ('jpeg files', '.jpg .jpeg'), ('.png files', '.png'), ('.gif files', '.gif'), ('.tif files', '.tif')])

        # A way to actually open the file (as a stream??)
        # self.currentdir = askopenfile(mode='r', self.currentpath)

        # kill window to get out of the master.mainloop()
        self.currentpaths.append(path)
        self.updatedirlist()
        #self.master.destroy()
    
    def pickdirectory(self):
        # Call "Choose directory..." dialog
        path = askdirectory()

        # should check if path is not empty and stuff, here

        # for each file you find, add it
        filelist = []
        for (dirpath, dirnames, filenames) in os.walk(path):
            for filetemp in filenames:
                if filetemp.endswith(".jpg") or filetemp.endswith(".jpeg") \
                or filetemp.endswith(".png") or filetemp.endswith(".gif")  \
                or filetemp.endswith(".tif"):
                    filelist.append(dirpath+"/"+filetemp)

        # add files to the list        
        self.currentpaths += filelist

        # update our list
        self.updatedirlist()

    def updatedirlist(self):
        # this function updates the list by:
        # 1 - clearing the entire list
        # 2 - adding the entries in currentpaths one by one
        self.lb.delete(0,END)
        for item in self.currentpaths:
            self.lb.insert(END, item)

    def clearselected(self):
        # subtract the selection from our paths
        self.currentpaths = list(set(self.currentpaths) - set([self.lb.get(x) for x in self.lb.curselection()]))
        self.updatedirlist()
        
    def audioscrobbler(self):
        ws = audioscrobblerws.Webservice()
        user = ws.GetUser('abhin4v')
        friends = user.friends
        friendNames = [f.username for f in friends]
        print friendNames